////////////////////////////////////////////////////////////////////////////////
// This file is part of the Mist compiler.                                    //
//                                                                            //
// Copyright (C) 2009 The Mist project                                        //
//                                                                            //
// For a list of contributors, see the COMMITTERS file.                       //
//                                                                            //
// The Mist compiler is free software: you can redistribute it and/or modify  //
// it under the terms of the GNU General Public License as published by       //
// the Free Software Foundation, either version 3 of the License, or          //
// (at your option) any later version.                                        //
//                                                                            //
// The Mist compiler is distributed in the hope that it will be useful,       //
// but WITHOUT ANY WARRANTY; without even the implied warranty of             //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              //
// GNU General Public License for more details.                               //
//                                                                            //
// You should have received a copy of the GNU General Public License          //
// in the COPYING file. If not, see <http://www.gnu.org/licenses/>.           //
////////////////////////////////////////////////////////////////////////////////

///
/// \file
/// Contains several handy functions related to strings.
///

#ifndef COMMON_STRING_HPP
#define COMMON_STRING_HPP

////////////////////////////////////////////////////////////////////////////////
// Includes                                                                   //
////////////////////////////////////////////////////////////////////////////////

#include <sstream>
using std::ostringstream;
using std::istringstream;

#include <string>
using std::string;

#include <vector>
using std::vector;

#include "common/assert.hpp"

////////////////////////////////////////////////////////////////////////////////
// Function Declarations                                                      //
////////////////////////////////////////////////////////////////////////////////

/// \return the value \a val converted from type \a T to type \c string
///
template <class T>
string to_string(T val);

/// \return the value \a str converted from type \c string to type \a T
///
template<class T>
T to(const string& str);

/// \return true , if the \c string \a str can be converted to type \a T
///         false, otherwise
///
template<class T>
bool is(const string& str);

/// Combine all items in \a list into one string, separating them with
/// \a separator. \a T must be a type implicitly convertable to \c string.
///
template<class T>
string implode(const vector<T>& list, string separator);

/// Combine all items in \a list into one string, separating them with
/// \a separator (the last separator using \a last instead). \a T must be a
/// type implicitly convertable to \c string.
///
template<class T>
string implode(const vector<T>& list, string separator, string last);

/// \return \a code, but with all special characters escaped, so it may be used
///         as the content of a C++/Mist string.
///
string escape_string(const string& code);

////////////////////////////////////////////////////////////////////////////////
// Template Function Implementations                                          //
////////////////////////////////////////////////////////////////////////////////

template <class T>
string to_string(T val) {
	ostringstream strout;
	strout << val;
	return strout.str();
}

template <class T>
T to(const string& str) {
	istringstream strin(str);
	T result;
	strin >> result;
	assert(strin && strin.eof());
	return result;
}

template<class T>
bool is(const string& str) {
	istringstream strin(str);
	T temp;
	strin >> temp;
	return strin && strin.eof();
}

template<class T>
string implode(const vector<T>& list, string separator) {
	string result("");
	
	for (uint i = 0; i < list.size(); ++i) {
		result += to_string(list[i]);
		if (i + 1 < list.size())
			result += separator;
	}
	
	return result;
}

template<class T>
string implode(const vector<T>& list, string separator, string last) {
	string result("");
	
	for (uint i = 0; i < list.size(); ++i) {
		result += to_string(list[i]);
		if (i + 2 < list.size())
			result += separator;
		else if (i + 1 < list.size())
			result += last;
	}
	
	return result;
}

#endif // COMMON_STRING_HPP
